﻿package com.PlantVsZombies.Plants;

import java.awt.Graphics;
import java.awt.Image;
import java.awt.Point;

import javax.swing.ImageIcon;

import com.PlantVsZombies.constant.Constant;
import com.PlantVsZombies.constant.PlantType;

/**
 * 
 * 植物卡片
 * 
 * @author Administrator
 *
 */
public class SeedCard implements Constant {

	private float percent;

	private static Image allSeed = new ImageIcon("images/card.jpg").getImage();
	private static Image allSeedDark = new ImageIcon("images/card_hh.jpg").getImage();

	private Point pos;
	private Point coord;
	// 植物类型
	private PlantType plantType;
	// 卡片是否可用
	private boolean enable = false;
	// 植物所消耗的阳光数
	private int cost;

	public PlantType getPlantType() {
		return plantType;
	}

	private int freezingSeconds;
	private int count;

	public boolean isEnable() {
		return enable;
	}

	public void setEnable(boolean enable) {
		this.enable = enable;
	}

	public int getCost() {
		return cost;
	}

	public void setCost(int cost) {
		this.cost = cost;
	}

	public int getCount() {
		return count;
	}

	public void setCount(int count) {
		this.count = count;
	}

	public float getPercent() {
		return percent;
	}

	public void setPercent(float percent) {
		this.percent = percent;
	}

	public SeedCard(Point pos, Point coord) {
		this.pos = pos;
		this.coord = coord;
		plantType = seedMap[coord.y][coord.x]; // 得到相应的植物类型对象
		percent = .5f; // 设置植物列表刷新的节奏

		initFreezingTime(plantType); // 设置每种植物不可用时间
		count = 0;
	}

	public boolean mouseIn(int x, int y) {
		if ((x > pos.x) && (x < pos.x + CARD_WIDTH) && (y > pos.y)
				&& (y < pos.y + CARD_HEIGHT)) {
			return true;
		}
		return false;
	}

	// 设置植物冻结时间
	private void initFreezingTime(PlantType plantType) {
		freezingSeconds = 300; // freezingSeconds越大则刷新越慢
		switch (plantType) {
		case SunFlower:
			freezingSeconds = 5;
			cost = 50;
			break;
		case SingleBullet:
			freezingSeconds = 10;
			cost = 100;
			break;
		case Cherry:
			freezingSeconds = 30;
			cost = 150;
			break;
		case SmallStone:
			freezingSeconds = 20;
			cost = 50;
			break;
		case QH1:
			freezingSeconds = 25;
			cost = 200;
			break;
		case QH2:
			freezingSeconds = 30;
			cost = 250;
			break;
		case QH3:
			freezingSeconds = 35;
			cost = 300;
			break;
		case DoubBullet:
			freezingSeconds = 500;
			break;
		}

	}

	public void reset() {
		count = 0;
		enable = false;
	}

	// 产生延迟，生成目标和源图的起始坐标Y的递增量
	public void seedUpdate() {
		++count;
		percent = ((float) count) / (freezingSeconds * DEFAULT_FPS);
		if (count >= freezingSeconds * DEFAULT_FPS) {
			count = 0;
			enable = true;
		}
	}

	public void draw(Graphics g) {

		int picX = CARD_WIDTH * coord.x + CARD_GAP_W * coord.x; // 得到源图的起始坐标X
		int picY = CARD_HEIGHT * coord.y + CARD_GAP_H * coord.y; // 得到源图的起始坐标Y
		int topH = (int) (CARD_HEIGHT * percent);
		g.drawImage(allSeed, pos.x, pos.y, pos.x + CARD_WIDTH, pos.y + topH,
				picX, picY, picX + CARD_WIDTH, picY + topH, null);
		g.drawImage(allSeedDark, pos.x, pos.y + topH, pos.x + CARD_WIDTH, pos.y
				+ CARD_HEIGHT, picX, picY + topH, picX + CARD_WIDTH, picY
				+ CARD_HEIGHT, null);
	}

}
